package com.mikufufu.blog.controller.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mikufufu.blog.common.entity.AjaxResult;
import com.mikufufu.blog.model.dto.api.ArticleParam;
import com.mikufufu.blog.model.vo.admin.ArticleVO;
import com.mikufufu.blog.model.vo.api.ArticleApiVO;
import com.mikufufu.blog.service.ArticleService;
import com.mikufufu.blog.service.CollectionService;
import com.mikufufu.blog.utils.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/article")
@Api(tags = "客户端的文章接口")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private CollectionService collectionService;

    @GetMapping("/detail")
    @ApiOperation(value = "获取文章详情", notes = "通过文章id查询指定文章的详细信息")
    @ApiImplicitParam(name = "id", value = "文章id", required = true)
    public AjaxResult<ArticleApiVO> detail(Integer id) {
        ArticleVO article = articleService.getArticleById(id);
        return AjaxResult.data(BeanUtil.copy(article, ArticleApiVO.class));
    }

    @PostMapping("/page")
    @ApiOperation(value = "查询文章列表 分页", notes = "根据筛选条件分页查询文章数据")
    public AjaxResult<IPage<ArticleApiVO>> page(@RequestBody @Validated ArticleParam article) {
        IPage<ArticleApiVO> page = articleService.getArticlePage(article);
        return AjaxResult.data(page);
    }

    @GetMapping("/getCollectionArticleList")
    @ApiOperation(value = "获取合集文章列表")
    public AjaxResult<List<ArticleApiVO>> getCollectionArticleList(Integer collectionId) {
        List<ArticleApiVO> articleVoS = BeanUtil.copyList(collectionService.getCollectionArticleList(collectionId), ArticleApiVO.class);
        return AjaxResult.data(articleVoS);
    }

    @GetMapping("/getTopArticle")
    @ApiOperation(value ="获取置顶文章", notes = "获取置顶文章")
    public AjaxResult<ArticleApiVO> getTopArticle() {
        ArticleVO article = articleService.getTopArticle();
        return AjaxResult.data(BeanUtil.copy(article, ArticleApiVO.class));
    }

}
